var addnewclient = (function (anc){
    var initials = false, cID=0;

    anc.reset = function(){
        $("#txtName").val("");
        var temp = $("#selTimezone option").eq(0).val();
        $("#selTimezone option[value='" + temp + "']").attr("selected","selected");
        $("#selTimezone").selectmenu('refresh', true);
        $("#txtClientcode").val("");
        $("#txtStreetaddress").val("");
        $("#txtCity").val("");
        $("#txtState").val(""),
        temp = $("#selCountry option").eq(0).val();
        $("#selCountry option[value='" + temp + "']").attr("selected","selected");
        $("#selCountry").selectmenu('refresh', true);
        $("#txtZIPcode").val("");
        $("#txtEmail1").val("");
        $("#txtEmail2").val("");
        $("#txtPhone1").val("");
        $("#txtPhone2").val("");
        $("#txtPhone3").val("");
        $("#txtPhone4").val("");
        $("#txtFax").val("");
        $("#txtNoteClient").val("");
        $("#txtWebsite").val("");
        $("#txtVATNumber").val("");
        $("#txtGeneralInfo").val("");
        temp = $("#selClientcurrency option").eq(0).val();
        $("#selClientcurrency option[value='" + temp + "']").attr("selected","selected");
        $("#selClientcurrency").selectmenu('refresh', true);
        $("#txtMinimumfee").val(0);
        $("#radio-choice-1").attr('checked','checked');
        $("#txtIDTValue1").val("10");
        $("#txtIDTValue2").val("10");
        $("#txtIDTValue3").val("10");
        $("#txtIDTValue4").val("10");
        $("#txtJobReach").val("0");
        $("#txtJobReach").attr("disabled",true);
        $("#txtWait").val("0");
        $("#txtWait").attr("disabled",true);
        $("#checkbox-mini-0").attr("disabled",true);
        $("#radio-choice-11").attr('checked','checked');
        $("#checkbox-mini-0").removeAttr('checked');
    }
    anc.edit = function( clientID ){
        dataBase.getClientByID(clientID, function (data){
            $("#txtName").val(data.Name);
            $("#selTimezone option[value='" + data.TimeZone + "']").attr("selected","selected");
            $("#selTimezone").selectmenu('refresh', true);
            $("#txtClientcode").val(data.ClientCode);
            $("#txtStreetaddress").val(data.StreetAddress);
            $("#txtCity").val(data.City);
            $("#txtState").val(data.State),
            $("#selCountry option[value='" + data.CountryID + "']").attr("selected","selected");
            $("#selCountry").selectmenu('refresh', true);
            $("#txtZIPcode").val(data.ZipCode);
            $("#txtEmail1").val(data.Email1);
            $("#txtEmail2").val(data.Email2);
            $("#txtPhone1").val(data.Phone1);
            $("#txtPhone2").val(data.Phone2);
            $("#txtPhone3").val(data.Phone3);
            $("#txtPhone4").val(data.Phone4);
            $("#txtFax").val(data.Fax);
            $("#txtNoteClient").val(data.Note);
            $("#txtWebsite").val(data.WebSite);
            $("#txtVATNumber").val(data.VATNumber);
            $("#txtGeneralInfo").val(data.GeneralInfo);
            $("#selClientcurrency option[value='" + data.CurrencyID + "']").attr("selected","selected");
            $("#selClientcurrency").selectmenu('refresh', true);
            $("#txtMinimumfee").val(data.PaymentTerms.MinimumFee);
            if(data.PaymentTerms.InvoiceDateType === 1)
                $("#radio-choice-1").attr('checked','checked');
            if(data.PaymentTerms.InvoiceDateType === 2)
                $("#radio-choice-2").attr('checked','checked');
            if(data.PaymentTerms.InvoiceDateType === 3)
                $("#radio-choice-3").attr('checked','checked');
            if(data.PaymentTerms.InvoiceDateType === 4)
                $("#radio-choice-4").attr('checked','checked');
            $("#txtIDTValue1").val(data.PaymentTerms.IDTValue1);
            $("#txtIDTValue2").val(data.PaymentTerms.IDTValue2);
            $("#txtIDTValue3").val(data.PaymentTerms.IDTValue3);
            $("#txtIDTValue4").val(data.PaymentTerms.IDTValue4);
            $("#txtJobReach").val(data.InvoicingPolicy.AfterTotal);
            $("#txtWait").val(data.InvoicingPolicy.Days);
            if(data.InvoicingPolicy.InvoiceType === "true")
                $("#radio-choice-11").attr('checked','checked');
            else
                $("#radio-choice-12").attr('checked','checked');
            if(data.InvoicingPolicy.Wait === "true")
                $("#checkbox-mini-0").attr('checked','checked');
            cID = clientID;
        });
    };
    anc.initAddnewclient = function (){
        var i= 0, num,str=[];

        if(!initials){
            $("#btnAddNewClientCancel").click( function (){
                if(cID === 0){
                    $.mobile.changePage("#pnMainPage");
                }else{
                    $.mobile.changePage("#pnClient");
                }
            });
            $("#radio-choice-11").on( "change", function(event, ui) {
                if($("#radio-choice-12").is(':checked') === true){
                    $("#txtJobReach").textinput('enable');
                    $("#checkbox-mini-0").checkboxradio('enable');
                    if($("#checkbox-mini-0").removeAttr("checked") === "checked")
                        $("#txtWait").textinput('enable');
                }else{
                    $("#txtJobReach").textinput('disable');
                    $("#checkbox-mini-0").checkboxradio('disable');
                    $("#txtWait").textinput('disable');
                }

            });
            $("#radio-choice-12").on( "change", function(event, ui) {
                if($("#radio-choice-12").is(':checked') === true){
                    $("#txtJobReach").textinput('enable');
                    $("#checkbox-mini-0").checkboxradio('enable');
                    if($("#checkbox-mini-0").removeAttr("checked") === "checked")
                        $("#txtWait").textinput('enable');
                }else{
                    $("#txtJobReach").textinput('disable');
                    $("#checkbox-mini-0").checkboxradio('disable');
                    $("#txtWait").textinput('disable');
                }

            });
            $("#checkbox-mini-0").on( "change", function(event, ui) {
                if($("#checkbox-mini-0").attr("checked") === "checked")
                    $("#txtWait").textinput('enable');
                else
                    $("#txtWait").textinput('disable');
            });
            $("#btnAddNewClientSave").click( function (){
                var newData = {
                    ClientID : cID,
                    Name : $("#txtName").val(),
                    TimeZone : parseInt($("#selTimezone option:selected").val()),
                    ClientCode:$("#txtClientcode").val(),
                    StreetAddress : $("#txtStreetaddress").val(),
                    City : $("#txtCity").val(),
                    State : $("#txtState").val(),
                    CountryID : parseInt($("#selCountry option:selected").val()),
                    ZipCode : $("#txtZIPcode").val(),
                    Email1 : $("#txtEmail1").val(),
                    Email2 : $("#txtEmail2").val(),
                    Phone1 : $("#txtPhone1").val(),
                    Phone2 : $("#txtPhone2").val(),
                    Phone3 : $("#txtPhone3").val(),
                    Phone4 : $("#txtPhone4").val(),
                    Fax : $("#txtFax").val(),
                    Note : $("#txtNoteClient").val(),
                    WebSite : $("#txtWebsite").val(),
                    VATNumber : $("#txtVATNumber").val(),
                    GeneralInfo : $("#txtGeneralInfo").val(),
                    CurrencyID : parseInt($("#selClientcurrency option:selected").val()),
                    PaymentTerms:{
                        MinimumFee:parseInt($("#txtMinimumfee").val()),
                        InvoiceDateType:1,
                        IDTValue1:parseInt($("#txtIDTValue1").val()),
                        IDTValue2:parseInt($("#txtIDTValue2").val()),
                        IDTValue3:parseInt($("#txtIDTValue3").val()),
                        IDTValue4:parseInt($("#txtIDTValue4").val()),
                        Note:$("#txtAdditionalNote").val()
                    },
                    InvoicingPolicy:{
                        InvoiceType:$("#radio-choice-11").is(':checked'),
                        AfterTotal:$("#txtJobReach").val(),
                        Wait:$("#checkbox-mini-0").is(':checked'),
                        Days:parseInt($("#txtWait").val())
                    }
                };
                if($("#radio-choice-1").is(':checked'))
                    newData.PaymentTerms.InvoiceDateType = 1;
                if($("#radio-choice-2").is(':checked'))
                    newData.PaymentTerms.InvoiceDateType = 2;
                if($("#radio-choice-3").is(':checked'))
                    newData.PaymentTerms.InvoiceDateType = 3;
                if($("#radio-choice-4").is(':checked'))
                    newData.PaymentTerms.InvoiceDateType = 4;

                if(cID === 0){
                    dataBase.insertClient(newData, function (){
                        main.updateList();
                        $.mobile.changePage("#pnMainPage");
                    });
                }else{
                    dataBase.editClient(newData, function(){
                        main.updateList();
                        $.mobile.changePage("#pnClient");
                    });
                }
            });
            num = 13;
            for(i=0;i<27;i+=1){
                if(num>0){
                    str.push("<option value='" + num + "'>UTC +" + num + ":00</option>");
                    str.push("<option value='" + (num+0.5) + "'>UTC +" + (num-1) + ":30</option>");
                }else if(num===0){
                    str.push("<option value='0'>UTC  0:00</option>");
                    str.push("<option value='-0.5'>UTC -0:30</option>");
                    str.push("<option value='-1'>UTC -1:00</option>");
                    num-=1;
                }else{
                    str.push("<option value='" + (num+0.5) + "'>UTC " + (num+1) + ":30</option>");
                    str.push("<option value='" + num + "'>UTC " + num + ":00</option>");
                }
                num-=1;
            }
            $("#selTimezone").html(str.join(''));

            dataBase.getCurrencyList(function (data){
               var i=0; num = data.length;
                str=[];
                for(i;i<num;i+=1){
                    str.push("<option value='" + data[i].CurrencyID + "'>" + data[i].Currency + "</option>");
                }
                $("#selClientcurrency").html(str.join(''));
                $("#selClientcurrency").selectmenu();
                $("#selClientcurrency option[value='" + data[0].CurrencyID + "']").attr("selected",true);
                //$("#selClientcurrency").selectmenu('refresh');

            });
            dataBase.getCountriesList(function(data){
                var i=0; num = data.length;
                str=[];
                for(i;i<num;i+=1){
                    str.push("<option value='" + data[i].CountryID + "'>" + data[i].Name + "</option>");
                }
                $("#selCountry").html(str.join(''));
                $("#selCountry").selectmenu();
                $("#selCountry option[value='" + data[0].CountryID + "']").attr("selected",true);
                //$("#selCountry").selectmenu('refresh');
            });
            initials = true;
        }else{
            anc.reset();
        }
        cID = 0;

    }
    anc.initAddnewclient();

    return anc;
}(addnewclient || {}));
